﻿using CBS.APP;
using CBS.Desktop.FormPage;
using CBS.Model.ManageModel;
using CBS.Model.ViewModel;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace CBS.Desktop.ViewPartial
{
    /// <summary>
    /// GradeManage.xaml 的交互逻辑
    /// </summary>
    public partial class GradeManage : UserControl
    {
        public Pagination pager = new() { rows = 10, page = 1, sidx = nameof(GradeViewModel.GradeId), sord = "desc" };
        public GradeManage()
        {
            InitializeComponent();

            FlushGrid();
        }

        private void UserControl_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.Key == Key.Enter)
            {
                // 调用处理方法
                pager.page = 1;
                pg_DataList.PageIndex = 1;
                FlushGrid();
                e.Handled = true; // 如果需要，标记事件已处理
            }
        }
        private void pg_DataList_PageUpdated(object sender, HandyControl.Data.FunctionEventArgs<int> e)
        {
            pager.page = e.Info;

            FlushGrid();
        }

        public async void FlushGrid()
        {
            try
            {
                btn_Search.IsEnabled = false;
                btn_Add.IsEnabled = false;

                string key = tb_SearchKey.Text.Trim();
                var grades = await Task.Run(() => GradeApp.GetList(pager, key));

                this.Dispatcher.Invoke(new Action(() =>
                {
                    pg_DataList.MaxPageCount = pager.total;
                    dgr_DataList.ItemsSource = grades;

                    btn_Search.IsEnabled = true;
                    btn_Add.IsEnabled = true;
                }));
            }
            catch (Exception ex)
            {
                MessageBox.Show(Window.GetWindow(this), ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
                btn_Search.IsEnabled = true;
                btn_Add.IsEnabled = true;
            }

        }

        private void btn_Add_Click(object sender, RoutedEventArgs e)
        {
            AddGrade addGrade = new()
            {
                Owner = Window.GetWindow(this)
            };
            var dialogResult = addGrade.ShowDialog();
            if (dialogResult.HasValue && dialogResult.Value)
            {
                pager.page = 1;
                pg_DataList.PageIndex = 1;
                FlushGrid();
            }
        }

        //private void btn_Delete_Click(object sender, RoutedEventArgs e)
        //{
        //    string key = tb_SearchKey.Text.Trim();
        //    if (CourseApp.BatchDeleteCourse(key))
        //    {
        //        pager.page = 1;
        //        FlushGrid();
        //    }
        //}

        private void btn_Search_Click(object sender, RoutedEventArgs e)
        {
            FlushGrid();
        }

        private void dg_Delete(object sender, RoutedEventArgs e)
        {
            try
            {
                var tmp = sender as System.Windows.Controls.Button;
                var data = tmp.DataContext as GradeViewModel;
                if (GradeApp.DeleteGrade(data.GradeId, out string msg))
                {
                    FlushGrid();
                }
                else
                {
                    throw new Exception(msg);
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(Window.GetWindow(this), ex.Message, "发生错误！", MessageBoxButton.OK, MessageBoxImage.Error);
            }

        }
    }
}
